package com.yinhe.music.yhmusic.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.yinhe.music.common.utils.DisplayUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.MyHeaderAdapter;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.HeaderBean;
import com.yinhe.music.yhmusic.utils.NetworkUtils;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.MyItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import static com.yinhe.music.yhmusic.constants.IConstants.COLLECT_MENU;

public class MenuManagerDialog {

    public interface MenuManagerDialogListener {
        void menuManagerDialogDidManager(BuildBean dialog);

        void menuManagerDialogDidCreate(String mName, BuildBean dialog);
    }

    private MenuManagerDialogListener mLisener;
    private Context mContext;
    private int mType;
    private BuildBean dialog;

    private final int[] menuIcon = {R.drawable.ic_more_add, R.drawable.ic_more_singer};
    private final String[] menuIconName = {"创建新歌单", "歌单管理"};

    public MenuManagerDialog(Activity context, int type, MenuManagerDialogListener listener) {
        mContext = context;
        mLisener = listener;
        mType = type;
        init();
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public BuildBean getDialog() {
        return dialog;
    }

    private void init() {
        View view = View.inflate(mContext, R.layout.dialog_collection, null);
        dialog = DialogUIUtils.showCustomBottomAlert(mContext, view);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        TextView title = view.findViewById(R.id.title);
        title.setText(mType == IConstants.COLLECT_MENU
                ? "收藏的歌单"
                : "创建的歌单");
        MyHeaderAdapter headAdapter = new MyHeaderAdapter(getData(mType));

        headAdapter.setOnItemClickListener((adapter, view1, position) -> {
            if (NetworkUtils.isConnectInternet(mContext) && mLisener != null) {
                switch (position) {
                    case 0:
                        if (mType == IConstants.CREATE_MENU) {
                            DialogUIUtils.dismiss(dialog);
                            EditDialog editDialog = new EditDialog(mContext, this::createMenu);
                            editDialog.show();
                        } else {
                            mLisener.menuManagerDialogDidManager(dialog);
                            DialogUIUtils.dismiss(dialog);
                        }
                        break;
                    case 1:
                        mLisener.menuManagerDialogDidManager(dialog);
                        DialogUIUtils.dismiss(dialog);
                        break;
                    default:
                        break;
                }
            }
        });
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
        recyclerView.setAdapter(headAdapter);
        recyclerView.addItemDecoration(new MyItemDecoration(mContext,
                DividerItemDecoration.VERTICAL_LIST, -1,
                DisplayUtil.dp2px(MusicApplication.getContext(), 60)));
    }

    private List<HeaderBean> getData(int type) {
        List<HeaderBean> list = new ArrayList<>();
        for (int i : menuIcon) {
            HeaderBean headerBean = new HeaderBean(menuIconName[i], 0, menuIcon[i]);
            list.add(headerBean);
        }
        if (type == COLLECT_MENU) {
            list.remove(0);
        }
        return list;
    }

    private void createMenu(String name) {
        if (dialog != null) {
            mLisener.menuManagerDialogDidCreate(name, dialog);
        }
    }
}
